#include <cassert>
#include <iostream>
#include <fstream>   // file I/O
#include <cstdlib>
#include <math.h>
#include "tga.h"
using namespace std;




/***********************************
 * CONSTRUCTORES ******************
 *********************************/

TGA::TGA(const char *filename){
    FILE *file;
	unsigned char type[4];
	unsigned char info[6];

    file = fopen(filename, "rb");

    if (!file){
		cout<<"file not found"<<endl;
		return;
	}

	fread (&type, sizeof (char), 3, file);
	fseek (file, 12, SEEK_SET);
	fread (&info, sizeof (char), 6, file);

	//image type either 2 (color) or 3 (greyscale)
	if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
	{
		fclose(file);
		cout<<"bad type"<<endl;
		return;
	}

	this->width = info[0] + info[1] * 256;
	this->height = info[2] + info[3] * 256;
	this->byteCount = info[4] / 8;

	if (this->byteCount != 3 && this->byteCount != 4) {
		fclose(file);
		return;
	}

	long imageSize = this->width * this->height * this->width * this->byteCount;

	//allocate memory for image data
	this->data = new unsigned char[imageSize];

	//read in image data
	fread(this->data, sizeof(unsigned char), imageSize, file);

	//close file
	fclose(file);
}

pair< int, int > TGA::getMax(){
	int max = 0;
	int pi, pj;

	for(int i = 0;i<this->height;i++){
		for(int j = 0; j<this->width; j++){
		//	cout<<img.data[i]<<endl;
			int pixel = i*this->width + j;
			int offset = pixel*3;
			//printf("(%u,%u,%u)",img.data[offset+0],img.data[offset+1],img.data[offset+2]);
			//cout<<endl;
			int suma = this->data[offset+0] + this->data[offset+1] + this->data[offset+2];
			if(suma > max){
				max = suma;
				pi = i;
				pj = j;
			}
		}
	}
	int pixel = pi*this->width + pj;
	int offset = pixel*3;
	cout<<"Punto máximo: ("<<this->height-pi<<","<<pj<<") = RGB ";
	printf("(%u,%u,%u)",this->data[offset+0],this->data[offset+1],this->data[offset+2]);
	cout<<endl;
	pair< int, int > res(this->height-pi, pj);
	return res;
}

int TGA::getRadio(){
	int pixeles = -1;
	int tmp;
	for(int i = 0;i<this->height;i++){
		tmp = 0;
		for(int j = 0; j<this->width; j++){
		//	cout<<img.data[i]<<endl;
			int pixel = i*this->width + j;
			int offset = pixel*3;
			//printf("(%u,%u,%u)",img.data[offset+0],img.data[offset+1],img.data[offset+2]);
			//cout<<endl;
			int suma = this->data[offset+1] + this->data[offset+2] + this->data[offset+3];
			if(suma > 600){
				tmp++;
			}
		}

		if(tmp>=pixeles){
			pixeles = tmp;
		}else{
			return pixeles/2;
		}
	}
	return pixeles/2;
}

pair< int, int > TGA::getCentro(int radio){
	int pi, pj;
	int pimin = this->height;
	int pjmin = this->width;
	
	for(int i = 0;i<this->height;i++){
		for(int j = 0; j<this->width; j++){	
			if (getPixel(i,j)[0] != 0){
				if (pimin > i){pimin=i;}
				if (pjmin > j){pjmin=j;}	
			}			
		}
	}	
	pi = pimin+radio;
	pj = pjmin+radio;
	pair< int, int > res(pi, pj);
	return res;
}


int* TGA::getPixel(int i, int j){
	int pixel = i*this->width + j;
	int offset = pixel*3;
	int* res = new int[3];
	res[0] = this->data[offset+0];
	res[1] = this->data[offset+1];
	res[2] = this->data[offset+2];
	return res;
}


